package controller;

import controller.util.PaginationHelper;
import remoteEntity.Comment;
import remoteEntity.User;
import facade.CommentFacade;
import java.io.IOException;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import manager.CommentManager;


/**
 * CommentController
 *
 * JSF Managed backing bean containing model showing logic for the comment page.
 * Also handles the posting of new comments.
 */
@ManagedBean
@ViewScoped
public class CommentController extends PaginationController implements Serializable {
    @EJB
    private CommentManager commentManager;
    @EJB
    private CommentFacade commentFacade;
    @ManagedProperty(value = "#{userController}")
    private UserController userController;
    private Integer fileId;
    private String message;

    public String getMessage() {
        return message;
    }

    public UserController getUserController() {
        return userController;
    }

    public void setUserController(UserController userController) {
        this.userController = userController;
    }

    public Integer getFileId() {
        return fileId;
    }

    public void setFileId(Integer fileId) {
        this.fileId = fileId;
    }

    @PostConstruct
    public void init() {
        this.fileId = Integer.valueOf(FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap().get("id"));
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void postMessage() throws IOException {
        User user = userController.getUser();

        commentManager.addComment(this.message, userController.getUser().getId(), this.fileId);

        this.message = null;
        FacesContext.getCurrentInstance().getExternalContext().redirect("fileinfo?id=" + fileId);
    }
    
    public void removeComment(Comment comment){
         commentManager.removeComment(comment);

    }

    public CommentController() {
    }

    @Override
    protected CommentFacade getFacade() {
        return commentFacade;
    }

    @Override
    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    if (fileId != null) {
                        return getFacade().countFileId(fileId);
                    } else {
                        return getFacade().count();
                    }
                }

                @Override
                public DataModel createPageDataModel() {
                    int[] range = new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()};
                    if (fileId != null) {
                        return new ListDataModel(getFacade().findByFileId(fileId, range));
                    } else {
                        return new ListDataModel(getFacade().findRange(range));
                    }
                }
            };
        }
        return pagination;
    }
}
